package com.bebal.cashloan.controller;

import com.babel.util.result.PageResult;
import com.babel.util.result.WsResult;
import com.bebal.cashloan.entity.DemoEntity;
import com.bebal.cashloan.service.ModelService;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenhui on 2018/6/8.
 */
@Controller
public class PingController {

    @Resource
    ModelService modelService;
    @ResponseBody
    @RequestMapping(value = "/ping",method = RequestMethod.GET)
    public WsResult root(){
        return new WsResult().sucAndData("cloud-cashloan service runing");
    }


    @RequestMapping(value = "/demoList",method = RequestMethod.POST)
    public @ResponseBody
    WsResult getAll(@RequestParam(value="pn",defaultValue="1") Integer pn,
                    @RequestParam(value="ps",defaultValue="10") Integer ps){
        //获取第1页，5条内容，默认查询总数count
        /* 第一个参数是第几页；第二个参数是每页显示条数 */
        PageHelper.startPage(pn, ps);
        List<DemoEntity> postIn = modelService.queryAll();
        //用PageInfo对结果进行包装
        PageResult<DemoEntity> page = new PageResult<DemoEntity>();
        page.setRows(postIn);
        page.setTotal(modelService.count());

        return new WsResult().sucAndData(page);
    }
}
